//
//  PPLoginAndRegisterViewController.m
//  pickpic
//
//  Created by 胡文广 on 2017/3/18.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPLoginAndRegisterViewController.h"
#import "YKStepBtnView.h"
#import "PPLoginViewController.h"
#import "PPRegisterViewController.h"
@interface PPLoginAndRegisterViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *contentView;
@property (nonatomic, strong) NSArray *segmentTitle;
@property (nonatomic, strong) YKStepBtnView *stepView;

@property (nonatomic,strong) NSMutableArray *childVcs;
@end

@implementation PPLoginAndRegisterViewController
//-(void)viewWillAppear:(BOOL)animated{
//    [super viewWillAppear:animated];
//    
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
//}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (NSArray *)segmentTitle{
    if (!_segmentTitle) {
        _segmentTitle = @[@"登录",@"注册"];
    }
    return _segmentTitle;
}
- (void)viewDidLoad{
    
//    self.navigationItem.leftBarButtonItem = nil;
//     self.navigationController.navigationBar.tintColor = WHITE;
//    self.view.backgroundColor = WHITE;
//////    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.edgesForExtendedLayout = UIRectEdgeNone;;
    
//    UIView *statusBarView = [[UIView alloc]   initWithFrame:CGRectMake(0, -20,    self.view.bounds.size.width, 64)];
//    statusBarView.backgroundColor = [UIColor whiteColor];
//    [self.navigationController.navigationBar addSubview:statusBarView];
    
    

    
    _contentView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _contentView.delegate = self;
    _contentView.showsHorizontalScrollIndicator = NO;
    _contentView.showsVerticalScrollIndicator = NO;
    _contentView.pagingEnabled = YES;
    _contentView.bounces = NO;
    self.contentView = _contentView;
    [self.view addSubview:self.contentView];
    
    [self setupNavBar];

    [self setupChildViews];
    self.view.backgroundColor =WHITE;
    
}
- (NSMutableArray *)childVcs{

    if (!_childVcs) {
        _childVcs = [NSMutableArray array];
    }
    return _childVcs;
}
- (void)setupNavBar{
   
    
    self.stepView = [YKStepBtnView stepBtnViewWithCenter:CGPointMake(self.view.center.x, 22)];
        self.stepView.center = CGPointMake(self.view.center.x, 42);
    self.navigationItem.titleView = self.stepView;
    
    [self.stepView loadTitles:self.segmentTitle withClickedBlock:^(NSInteger index) {
        [self titleClick:index];
    }];

    self.stepView.selectedIndex = 0;
//    [self titleClick:0];
}
- (void)setupChildViews{
    
    PPLoginViewController *loginViewController = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"PPLoginViewController"];
    [self addChildViewController:loginViewController];
    loginViewController.view.frame = CGRectMake(0,0, self.view.width, SCREEN_HEIGHT-64);
    [_contentView addSubview:loginViewController.view];
    loginViewController.showKeyBoard = TRUE;
    
    [self.childVcs addObject:loginViewController];
    
    PPRegisterViewController *registerVc = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"PPRegisterViewController"];

    [self addChildViewController:registerVc];
    self.contentView.contentSize = CGSizeMake(self.segmentTitle.count * SCREEN_WIDTH,0);
    
    [self.childVcs addObject:registerVc];
  
}

// 点击标题按钮
- (void)titleClick:(NSInteger)selectedIndex
{
    CGFloat offsetX = selectedIndex * SCREEN_WIDTH;
    
    self.contentView.contentOffset = CGPointMake(offsetX, 0);
    PPBaseViewController *vc = self.childVcs[selectedIndex];
    vc.view.frame = CGRectMake(offsetX,0, self.contentView.width, SCREEN_HEIGHT-64);
    [self.contentView addSubview:vc.view];
    vc.showKeyBoard = true;
}
// 滚动完成的时候:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 获取当前的偏移量
    CGFloat offsetX = scrollView.contentOffset.x;
    // 获取页码
    int page = offsetX / scrollView.bounds.size.width;
    
    [self titleClick:page];
    self.stepView.selectedIndex = page;
}


@end
